package com.tools.ad;

import com.hm.base.android.mob.AMApplication;
import com.tools.ad.adscene.AdScene;
import com.tools.ad.adscene.func.FunctionScene;
import com.tools.ad.adscene.inner.CommonInterstitialScene;
import com.tools.ad.adscene.inner.CommonScene;
import com.tools.ad.adscene.inner.FuncInterstitialScene;
import com.tools.ad.adscene.inner.LandingScene;
import com.tools.ad.adscene.inner.SplashInterstitialScene;
import com.tools.ad.adscene.inner.TriggerScene;
import com.tools.ad.adscene.outer.AppOuterDeferScene;
import com.tools.ad.adscene.outer.AppOuterScene;
import com.tools.ad.adscene.outer.AppScene;
import com.tools.ad.adscene.outer.AutoCleanScene;
import com.tools.ad.adscene.outer.SwipeCloseScene;
import com.tools.ad.adscene.outer.SwipeRecentScene;
import com.tools.ad.adscene.outer.SwipeReplaceScene;
import com.tools.ad.adscene.outer.SwipeScene;
import com.tools.ad.adscene.outer.WeatherCloseScene;
import com.tools.ad.adscene.outer.WeatherFullScreenScene;
import com.tools.ad.adscene.outer.WeatherReplaceScene;
import com.tools.ad.adscene.outer.WeatherScene;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static com.tools.ad.ADConstant.HMPlaceId.APP_INSTALL_DEFER_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.APP_INSTALL_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.APP_OUTER_AD_UNION_ID_1;
import static com.tools.ad.ADConstant.HMPlaceId.APP_OUTER_AD_UNION_ID_2;
import static com.tools.ad.ADConstant.HMPlaceId.APP_OUTER_AD_UNION_ID_3;
import static com.tools.ad.ADConstant.HMPlaceId.APP_UNINSTALL_DEFER_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.APP_UNINSTALL_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.AUTO_CLEAN_DEFER_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.AUTO_CLEAN_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.HOME_BOOST_COMPLETED_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.HOME_BOOST_INTERSTITIAL_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.HOME_CPU_COMPLETED_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.HOME_CPU_INTERSTITIAL_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.HOME_POWER_COMPLETED_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.HOME_POWER_INTERSTITIAL_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.HOME_TRASH_COMPLETED_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.HOME_TRASH_INTERSTITIAL_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SCENE_COMPLETED_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SCENE_FUNCTION_BOOST_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SCENE_FUNCTION_COOL_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SCENE_FUNCTION_POWER_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SCENE_INTERSTITIAL_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SPLASH_TEST_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SPLASH_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SWIPE_CLOSE_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SWIPE_REPLACE_RECENT;
import static com.tools.ad.ADConstant.HMPlaceId.SWIPE_REPLACE_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.SWIPE_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.TRIGGER_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.WEATHER_CLOSE_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.WEATHER_FULL_SCREEN_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.WEATHER_REPLACE_UNION_ID;
import static com.tools.ad.ADConstant.HMPlaceId.WEATHER_UNION_ID;

//import static com.tools.ad.ADConstant.HMPlaceId.SPLASH_TEST_UNION_ID;


/**
 * 广告(功能，入口)展示场景
 */
public enum EntranceType {

    /**
     * 未知或未定义
     */
    UNKNOWN("unknown", null),

    /**
     * 场景-内存加速功能-扫描完成后-配置
     */
    FUNCTION_BOOST("function_boost", FunctionScene.class, SCENE_FUNCTION_BOOST_UNION_ID),

    /**
     * 场景-内存加速功能-扫描完成后-配置
     */
    FUNCTION_POWER("function_power", FunctionScene.class, SCENE_FUNCTION_POWER_UNION_ID),

    /**
     * 场景-内存加速功能-扫描完成后-配置
     */
    FUNCTION_COOL("function_cool", FunctionScene.class, SCENE_FUNCTION_COOL_UNION_ID),

    /**
     * 每个场景扫描完毕后，嵌入到无多个结果页面里面的广告
     */
    LANDING("landing", LandingScene.class, SCENE_COMPLETED_UNION_ID),

    /**
     * 每个场景扫描完毕后，嵌入到无多个结果页面里面的广告
     */
    LANDING_INTERSTITIAL("landing_inset", CommonInterstitialScene.class, SCENE_INTERSTITIAL_UNION_ID),

    /**
     * 闪屏页
     */
    SPLASH("splash", SplashInterstitialScene.class, new int[]{SPLASH_UNION_ID, SPLASH_TEST_UNION_ID}),

    /**
     * Trigger广告
     */
    TRIGGER("trigger", TriggerScene.class, TRIGGER_UNION_ID),

    /**
     * 首页垃圾清理完成页
     */
    HOME_TRASH_COMPLETED("home_trash", CommonScene.class, HOME_TRASH_COMPLETED_UNION_ID),

    /**
     * 首页垃圾清理完成页_插屏
     */
    HOME_TRASH_INTERSTITIAL("home_trash_inset", FuncInterstitialScene.class, HOME_TRASH_INTERSTITIAL_UNION_ID),

    /**
     * 首页耗电检测完成页
     */
    HOME_POWER_COMPLETED("home_power", CommonScene.class, HOME_POWER_COMPLETED_UNION_ID),

    /**
     * 首页耗电检测完成页_插屏
     */
    HOME_POWER_INTERSTITIAL("home_power_inset", FuncInterstitialScene.class, HOME_POWER_INTERSTITIAL_UNION_ID),

    /**
     * 首页CPU降温完成页
     */
    HOME_CPU_COMPLETED("home_cpu", CommonScene.class, HOME_CPU_COMPLETED_UNION_ID),

    /**
     * 首页CPU降温完成页_插屏
     */
    HOME_CPU_INTERSTITIAL("home_cpu_inset", FuncInterstitialScene.class, HOME_CPU_INTERSTITIAL_UNION_ID),

    /**
     * 首页手机加速完成页
     */
    HOME_BOOST_COMPLETED("home_boost", CommonScene.class, HOME_BOOST_COMPLETED_UNION_ID),

    /**
     * 首页手机加速完成页_插屏
     */
    HOME_BOOST_INTERSTITIAL("home_boost_inset", FuncInterstitialScene.class, HOME_BOOST_INTERSTITIAL_UNION_ID),


    /////////////////////////////// 以上是应用内，以下是应用外 ///////////////////////////////


    /**
     * 应用外广告
     */
    APP_OUTER_AD("app_outer", AppOuterScene.class, new int[]{APP_OUTER_AD_UNION_ID_1, APP_OUTER_AD_UNION_ID_2, APP_OUTER_AD_UNION_ID_3}),

    /**
     * 天气广告
     */
    WEATHER("weather", WeatherScene.class, WEATHER_UNION_ID),

    /**
     * 天气替换广告
     */
    WEATHER_REPLACE("weather_replace", WeatherReplaceScene.class, WEATHER_REPLACE_UNION_ID),

    /**
     * 天气全屏广告
     */
    WEATHER_FULL_SCREEN("weather_full_screen", WeatherFullScreenScene.class, WEATHER_FULL_SCREEN_UNION_ID),

    /**
     * 快滑广告
     */
    SWIPE("swipe", SwipeScene.class, SWIPE_UNION_ID),

    /**
     * 快滑替换
     */
    SWIPE_REPLACE("swipe_replace", SwipeReplaceScene.class, SWIPE_REPLACE_UNION_ID),

    /**
     * 安装应用
     */
    APP_INSTALL("app_install", AppScene.class, APP_INSTALL_UNION_ID),

    /**
     * 卸载应用
     */
    APP_UNINSTALL("app_uninstall", AppScene.class, APP_UNINSTALL_UNION_ID),

    /**
     * 自动清理
     */
    AUTO_CLEAN("auto_clean", AutoCleanScene.class, AUTO_CLEAN_UNION_ID),
    /**
     * 快划菜单关闭
     */
    SWIPE_CLOSE("swipe_close", SwipeCloseScene.class, SWIPE_CLOSE_UNION_ID),

    /**
     * 天气界面关闭
     */
    WEATHER_CLOSE("weather_close", WeatherCloseScene.class, WEATHER_CLOSE_UNION_ID),

    /**
     * 安装应用 (二次延时广告)
     */
    APP_INSTALL_DEFER("app_install_defer", AppOuterDeferScene.class, APP_INSTALL_DEFER_UNION_ID),

    /**
     * 卸载应用 (二次延时广告)
     */
    APP_UNINSTALL_DEFER("app_uninstall_defer", AppOuterDeferScene.class, APP_UNINSTALL_DEFER_UNION_ID),

    /**
     * 自动清理 (二次延时广告)
     */
    AUTO_CLEAN_DEFER("auto_clean_defer", AppOuterDeferScene.class, AUTO_CLEAN_DEFER_UNION_ID),

    /**
     * 应用外快划最近应用中简易icon+title广告
     */
    SWIPE_INDICATE_RECENT("swipe_replace_recent", SwipeRecentScene.class, SWIPE_REPLACE_RECENT);

    private final String mName;
    private String mSource;
    private int mPid;
    private int[] mPidArray;

    private Class<? extends AdScene> mAdStrategyScene;
    private AdScene sceneInstance;

    EntranceType(String mName, Class<? extends AdScene> adStrategyScene) {
        this.mName = mName;
        this.mPid = 0;
        this.mAdStrategyScene = adStrategyScene;
    }

    EntranceType(String mName, Class<? extends AdScene> adStrategyScene, int pid) {
        this.mName = mName;
        this.mPid = pid;
        this.mAdStrategyScene = adStrategyScene;
    }

    EntranceType(String mName, Class<? extends AdScene> adStrategyScene, int[] pidArray) {
        this.mName = mName;
        this.mPidArray = pidArray;
        this.mAdStrategyScene = adStrategyScene;
    }

    public String getName() {
        return mName;
    }

    public int getPid() {
        return mPid;
    }

    public int[] getPidArray() {
        return mPidArray;
    }

    public void setSource(String source) {
        mSource = source;
    }

    public String getSource() {
        return mSource;
    }

    public AdScene getAdScene() {
        if (mAdStrategyScene == null) {
            throw new NullPointerException("viewProducer in EntranceType can not be null");
        }

        if (sceneInstance != null) {
            return sceneInstance;
        }

        try {
            Constructor constructor = mAdStrategyScene.getConstructor(AMApplication.class, EntranceType.class);
            sceneInstance = (AdScene) constructor.newInstance(AMApplication.getInstance(), this);
            if (mPid == 0) {
                mPid = sceneInstance.getPlaceId();
            }
            return sceneInstance;

        } catch (InstantiationException e) {
            throw new IllegalArgumentException("the viewController in " + "EntranceType can not be instantiate");

        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException("the constructor of class " + mAdStrategyScene.getName() + " in EntranceType can not be access");

        } catch (NoSuchMethodException e) {
            e.printStackTrace();

        } catch (InvocationTargetException e) {
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
        }

        throw new IllegalStateException("unknown error");
    }

    public static EntranceType enumOf(String name) {
        if (name == null) {
            throw new NullPointerException("name == null");
        }
        EntranceType[] values = EntranceType.values();
        for (EntranceType value : values) {
            if (value.getName().equals(name)) {
                return value;
            }
        }
        throw new IllegalArgumentException(name + " is not a hold by constant in " + EntranceType.class.getName());
    }

}
